﻿using Estsh.Core.Base;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Wms.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    public class ReworkAndRepairService : BaseService<BaseEntity>, IReworkAndRepairService
    {
        private readonly IReworkAndRepairRepository repository;

        public ReworkAndRepairService(IReworkAndRepairRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        public List<Syspdline_Extend> GetSysPdlineList(string userId)
        {

            return repository.GetSysPdlineList(userId);
        }
        /// <summary>
        /// 根据工作中心来查询工单
        /// </summary>
        /// <param name="pdlineId"></param>
        /// <returns></returns>
        public List<MesWorkOrder_Extend> GetWorkOrderByPdlineId(string pdlineId)
        {
            return repository.GetWorkOrderByPdlineId(pdlineId);
        }

        /// <summary>
        /// 扫描条码 查询bom清单
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="cartonNo"></param>
        /// <returns></returns>
        public SetObjectDetail GetBomDetailDownInfo(string orderNo, string cartonNo)
        {

            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

            List<MesWorkOrder_Extend> mesWorksInfos = repository.GetWorkOrderByCartonNo(orderNo, cartonNo);//扫描箱条码，验证条码是否在工单中
            if (mesWorksInfos.Count == 0)
            {
                rfInfo.type = "NO";
                rfInfo.message = "【"+ cartonNo + "】：条码不在工单中，请确认！";
                return rfInfo;
            }
            if (mesWorksInfos[0].Qty==0 || mesWorksInfos[0].Qty == null)
            {
                rfInfo.type = "NO";
                rfInfo.message = "【" + cartonNo + "】：条码数量不可为0或空值，请确认！";
                return rfInfo;

            }
            List<SysBomDetail_Extend> bomDetails = new List<SysBomDetail_Extend>();

            repository.GetBomDetailListDown(mesWorksInfos[0].Qty,mesWorksInfos[0].PartId.ToString(), ref bomDetails);//查询工单的bom

            if (bomDetails.Count() > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "请选择需要返修的原材料！";
                rfInfo.wmsObjectDetail = bomDetails.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "NO";
                rfInfo.message = "请确认是否存在bom清单！";
            }
            return rfInfo;


        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="orderNo">工单号</param>
        /// <param name="cartonNo">箱条码</param>
        /// <param name="partIds">选中的原材料零件号id</param>
        /// <param name="loginId">操作人</param>
        /// <returns></returns>
        public SetObjectDetail SubmitOrder(string orderNo, string cartonNo,string partIds, string loginId)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

            string[] strPartIdArray = partIds.Split(',');//选中的零件号数组
            List<int> strPartIdList = new List<int>();//选中的零件号集合
            foreach (var item in strPartIdArray)
            {
                strPartIdList.Add(Convert.ToInt32(item));
            }
            List<MesWorkOrder_Extend> mesWorksInfos=new List<MesWorkOrder_Extend>();//工单数据集合
            List<Syspdline_Extend> syspdlineInfos = new List<Syspdline_Extend>();//产线数据集合
            List<SysBomDetail_Extend> reworkBomDetailInfos = new List<SysBomDetail_Extend>();//需要返修的原材料数据集合
            List<SysBomDetail_Extend> noReworkBomDetailInfos = new List<SysBomDetail_Extend>();//需要退回到线边库的原材料数据集合
            
            #region 条件验证

            #region 验证条码信息
            mesWorksInfos = repository.GetWorkOrderByCartonNo(orderNo, cartonNo);//扫描箱条码，验证条码是否在工单中
            syspdlineInfos = repository.GetPdlineByPdlineId(mesWorksInfos[0].PdlineId.ToString());//产线数据集合
            if (mesWorksInfos.Count == 0)
            {
                rfInfo.type = "NO";
                rfInfo.message = "【" + cartonNo + "】：条码不在工单中，请确认！";
                return rfInfo;
            }
            if (mesWorksInfos[0].Qty == 0 || mesWorksInfos[0].Qty == null)
            {
                rfInfo.type = "NO";
                rfInfo.message = "【" + cartonNo + "】：条码数量不可为0或空值，请确认！";
                return rfInfo;

            }

            List<SysStock_Extend> sysStockInfos = repository.GetStockByCartonNo(cartonNo);
            if ((mesWorksInfos[0].Qty - mesWorksInfos[0].CompleteQty) < sysStockInfos[0].Qty)//工单的需求数量-工单的完成数量<扫描条码的数量
            {
                rfInfo.type = "NO";
                rfInfo.message = "【" + cartonNo + "】：条码数量大于了工单的剩余数量，请确认！";
                return rfInfo;
            }
            #endregion

            List<SysBomDetail_Extend> bomDetails = new List<SysBomDetail_Extend>();

            repository.GetBomDetailListDown(sysStockInfos[0].Qty, mesWorksInfos[0].PartId.ToString(), ref bomDetails);//查询工单的bom
           
            reworkBomDetailInfos = bomDetails.Where(a=> strPartIdList.Exists(b=>a.ItemPartId.ToString().Contains(b.ToString()))).ToList();
            noReworkBomDetailInfos = bomDetails.Where(a => !strPartIdList.Exists(b => a.ItemPartId.ToString().Contains(b.ToString()))).ToList();
           

            #region 验证线边库的原材料数量，是否满足报废的原材料的数量






            #endregion


            #endregion









            if (bomDetails.Count() > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "请选择需要返修的原材料！";
                rfInfo.wmsObjectDetail = bomDetails.ConvertAll(s => (object)s);
                return rfInfo;
            }
            else
            {
                rfInfo.type = "NO";
                rfInfo.message = "请确认是否存在bom清单！";
                return rfInfo;
            }
          


        }

    }
}
